package com.aistar.service.resume.impl;

import com.aistar.mapper.*;
import com.aistar.model.dto.resume.ResumeInfoDTO;

import com.aistar.model.entity.*;
import com.aistar.service.resume.IResumeService;
import com.aistar.util.DateUtil;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
@Transactional
public class ResumeServiceImpl implements IResumeService {
    @Autowired
    private ResumeMapper resumeMapper;
    @Autowired
    private InternMapper internMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private AwardMapper awardMapper;
    @Autowired
    private EducationMapper educationMapper;

    @Override
    public ResumeInfoDTO loadResumeById(Integer id) {
        ResumeInfoDTO resumeInfoDTO = new ResumeInfoDTO();
        resumeInfoDTO.setResume(resumeMapper.findById(id));
        resumeInfoDTO.setAwardList(awardMapper.findByResumeId(id));
        resumeInfoDTO.setEducationList(educationMapper.findByResumeId(id));
        resumeInfoDTO.setInternList(internMapper.findByResumeId(id));
        resumeInfoDTO.setProjectList(projectMapper.findByResumeId(id));

        return resumeInfoDTO;
    }
    @Override
    public Integer updateResume(ResumeInfoDTO resumeInfoDTO) {
        Resume resume = resumeInfoDTO.getResume();


        resumeMapper.updateResume(resume);

        List<Award> awardList = resumeInfoDTO.getAwardList();
        if (null != awardList && awardList.size() > 0) {
            // 表示有award
            for (Award award : awardList) {
                awardMapper.updataAward(award);
            }
        }
        List<Education> educationList = resumeInfoDTO.getEducationList();
        if (null != educationList && educationList.size() > 0) {
            for (Education education : educationList) {
                educationMapper.updateEducation(education);
            }
        }
        List<Intern> internList = resumeInfoDTO.getInternList();
        if (null != internList && internList.size() > 0) {
            for (Intern intern : internList) {
                internMapper.updateIntern(intern);
            }
        }
        List<Project> projectList = resumeInfoDTO.getProjectList();
        if (null != projectList && projectList.size() > 0) {
            for (Project project : projectList) {
                projectMapper.updateProject(project);
            }
        }
        return 0;
    }

    @Override
    public Integer createResume(Integer emplyeeId) {
        Integer resumeId = getLatestResumeId(emplyeeId);
        Resume oldresume = resumeMapper.findById(resumeId);
        Resume resume = new Resume();
        resume.setName(oldresume.getName());
        resume.setSex(oldresume.getSex());
        resume.setResIdentity(oldresume.getResIdentity());
        resume.setResState(oldresume.getResState());
        resume.setArea(oldresume.getArea());
        resume.setBirthday(oldresume.getBirthday());
        resume.setPhone(oldresume.getPhone());
        resume.setEmail(oldresume.getEmail());
        resume.setUpdateTime(new Date());
        resume.setResumeAvatar(oldresume.getResumeAvatar());
        resume.setStatus(1);
        resume.setPrivacy(1);
        resume.setEmployeeId(oldresume.getEmployeeId());
        resume.setFrequency(0);
        resume.setExpectCity(oldresume.getExpectCity());
        resume.setExpectJob(oldresume.getExpectJob());
        resume.setExpectIndustry(oldresume.getExpectIndustry());
        resume.setSalaryRequirement(oldresume.getSalaryRequirement());
        resumeMapper.insertResume(resume);
        return resume.getId();
    }


//    @Override
//    public ResumeInfoDTO loadResumeByEmployeeId(Integer employeeId) {
//
//        Map<Integer, Long> resumeHashMap = new HashMap<>();
//        List<Resume> resumeList = resumeMapper.findByEmployeeId(employeeId);
//        for (Resume resume : resumeList) {
//            resumeHashMap.put(resume.getId(), resume.getUpdateTime().getTime());
//        }
//        Collection<Long> collection = resumeHashMap.values();
//        List<Map.Entry<Integer, Long>> list = new ArrayList(resumeHashMap.entrySet());
//        Collections.sort(list, (o1, o2) -> (int) (o1.getValue() - o2.getValue()));
//
//        ResumeInfoDTO resumeInfoDTO = loadResumeById(list.get(0).getKey());
////        resumeInfoDTO.setResume(resumeMapper.findById(list.get(0).getKey()));
////        resumeInfoDTO.setAwardList(awardMapper.findByResumeId(list.get(0).getKey()));
////        resumeInfoDTO.setEducationList(educationMapper.findByResumeId(list.get(0).getKey()));
////        resumeInfoDTO.setInternList(internMapper.findByResumeId(list.get(0).getKey()));
////        resumeInfoDTO.setProjectList(projectMapper.findByResumeId(list.get(0).getKey()));
//        return resumeInfoDTO;
//    }

    @Override
    public Integer updateByResume(Integer id, String name, String sex, String resIdentity, String resState, String birthday, String area, String phone, String email,String avatarName) {
        // 首先根据id获取简历
        Resume resume = resumeMapper.findById(id);
        resume.setName(name);
        resume.setSex(sex);
        resume.setResIdentity(resIdentity);
        resume.setResState(resState);
        //1999-09-03
        Date birth = DateUtil.parse(birthday, "yyyy-MM-dd");
        resume.setBirthday(birth);
        resume.setEmail(email);
        resume.setPhone(phone);
        resume.setArea(area);
        resume.setUpdateTime(new Date());
        resume.setResumeAvatar(avatarName);
        System.out.println(resume);
        return resumeMapper.updateResume(resume);
    }

    @Override
    public Integer updateByResumeOfJob(Integer id, String expectIndustry, String expectJob, String expectCity, String salaryRequirement) {
        Resume resume = resumeMapper.findById(id);
        resume.setExpectIndustry(expectIndustry);
        resume.setExpectJob(expectJob);
        resume.setExpectCity(expectCity);
        resume.setSalaryRequirement(salaryRequirement);
        return resumeMapper.updateResume(resume);
    }

    @Override
    public Integer updateByEducation(Integer id, String schoolName, String degree, String major, String startDate, String endDate, String experienceSchool) {
        Education education = educationMapper.findById(id);
        education.setSchoolName(schoolName);
        education.setDegree(degree);
        education.setMajor(major);
        Date start = DateUtil.parse(startDate, "yyyy-MM-dd");
        education.setStartDate(start);
        Date end = DateUtil.parse(endDate, "yyyy-MM-dd");
        education.setEndDate(end);
        education.setExperienceSchool(experienceSchool);
        return educationMapper.updateEducation(education);
    }

    @Override
    public Integer updateByIntern(Intern intern) {
        Intern oldIntern = internMapper.findById(intern.getId());
        intern.setResumeId(oldIntern.getResumeId());
        return internMapper.updateIntern(intern);
    }

    @Override
    public Integer updateByProject(Project project) {
        Project oldProject = projectMapper.fingById(project.getId());
        project.setResumeId(oldProject.getResumeId());
        return projectMapper.updateProject(project);
    }

    @Override
    public Integer updateByAward(Award award) {
        Award oldAward = awardMapper.findById(award.getId());
        award.setResumeId(oldAward.getResumeId());
        return awardMapper.updataAward(award);
    }

    @Override
    public Integer updateByResumeOfSummary(Integer id, String summary) {
        Resume resume = resumeMapper.findById(id);
        resume.setSummary(summary);
        return resumeMapper.updateResume(resume);
    }


    @Override
    public Integer updateUpdateTimeById(Integer id) {
        Resume resume = resumeMapper.findById(id);
        resume.setUpdateTime(new Date());
        return resumeMapper.updateUpdateTimeById(resume);
    }

//通过employeeId获取简历id
    @Override
    public Integer getLatestResumeId(Integer employeeId) {
//        Map<Integer, Long> resumeHashMap = new HashMap<>();
        List<Resume> resumeList = resumeMapper.findByEmployeeId(employeeId);
        if(resumeList==null || resumeList.size()==0)
            return null;
        resumeList.sort(((o1, o2) -> {
            if(o1.getUpdateTime().getTime()>o2.getUpdateTime().getTime())
                return -1;
            else if(o1.getUpdateTime().getTime()<o2.getUpdateTime().getTime())
                return 1;
            return 0;
        }));
        return resumeList.get(0).getId();
    }

    @Override
    public boolean judgeCreate(Integer employeeId) {
        // 首先根据这个求职者获取他所有的简历
        // 如果简历的个数>3则返回true -》 不能再创建简历了
        // 如果<3 则可以创建
        List<Resume> resume = resumeMapper.findByEmployeeId(employeeId);
        if(resume.size()>=3){
            return true;
        }
        return false;
    }

    @Override
    public Integer updateStatusById(Integer id) {
        return resumeMapper.updateResumeById(id);
    }



}
